package com.example.administrator.myapplication.set;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.set.PersonalCenter.PersonalCenterTestedActivity;
import com.example.administrator.myapplication.set.PersonalCenter.PersonalCenterTestedFragment;
import com.example.administrator.myapplication.set.about.AboutMainFragment;
import com.example.administrator.myapplication.tools.TestData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2018/1/14.
 */

public class UserinfoActivity extends Activity {
    private Toolbar toolbar;
    private EditText ed_userName;
    private EditText ed_height;
    private EditText ed_weight;
    private EditText ed_school;
    private EditText ed_major;
    private EditText ed_grade;
    private String id;

    private String height;
    private String username;
    private String weight;
    private String school;
    private String major;
    private String grade;
    private Button bt_setUserInfo;

    private String qheight;
    private String qusername;
    private String qweight;
    private String qschool;
    private String qmajor;
    private String qgrade;

    private BmobApplication setUserInfo_apps;

    class myHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                ed_userName.setText(qusername);
                ed_height.setText(qheight);
                ed_weight.setText(qweight);
                ed_grade.setText(qgrade);
                ed_major.setText(qmajor);
                ed_school.setText(qschool);
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_set_userinfo);
        toolbar = (Toolbar) findViewById(R.id.toolbar_userinfo);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("个人资料");
        toolbar.setTitleMarginStart(35);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ed_userName = (EditText) findViewById(R.id.ed_userinfo_username);
        ed_height = (EditText) findViewById(R.id.ed_userinfo_height);
        ed_weight = (EditText) findViewById(R.id.ed_userinfo_weight);
        ed_school = (EditText) findViewById(R.id.ed_userinfo_school);
        ed_major = (EditText)  findViewById(R.id.ed_userinfo_major);
        ed_grade = (EditText)  findViewById(R.id.ed_userinfo_grade);

        bt_setUserInfo = (Button)findViewById(R.id.bt_setUserInfo_submit);

        new  Thread(){
            @Override
            public void run() {
                query();
            }
        }.start();
        bt_setUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getinfo();
                if(grade==null||major==null||school==null){
                    Toast.makeText(UserinfoActivity.this,"有选项没有填写",Toast.LENGTH_SHORT).show();
                }
                else{
                    TestData testData =new TestData();
                    testData.setWeight(weight);
                    testData.setHeight(height);
                    testData.setGrade(grade);
                    testData.setSchool(school);
                    testData.setMajor(major);
                    testData.update(id, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                                Toast.makeText(UserinfoActivity.this,"更新成功!",Toast.LENGTH_SHORT).show();

                            }else{
                                System.out.println(e.getMessage());
                            }
                            finish();
                        }
                    });
                }
            }
        });
    }
    public void query(){
        File file =new File(this.getFilesDir(),"objectid.txt");
        try {
            BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            id=br.readLine();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BmobQuery<TestData> user =new BmobQuery<TestData>();
        user.getObject(id, new QueryListener<TestData>() {
            @Override
            public void done(TestData testData, BmobException e) {
                qheight=testData.getHeight();
                qweight=testData.getWeight();
                qusername=testData.getUsername();
                qmajor=testData.getMajor();
                qschool=testData.getSchool();
                qgrade=testData.getGrade();
                new myHandler().sendEmptyMessage(1);
            }
        });
    }

    public void getinfo(){
        height= TextUtils.isEmpty(ed_height.getText().toString())?qheight:ed_height.getText().toString();
        weight=TextUtils.isEmpty(ed_weight.getText().toString())?qweight:ed_weight.getText().toString();
        major=TextUtils.isEmpty(ed_major.getText().toString())?qmajor:ed_major.getText().toString();
        school=TextUtils.isEmpty(ed_school.getText().toString())?qschool:ed_school.getText().toString();
        grade=TextUtils.isEmpty(ed_grade.getText().toString())?qgrade:ed_grade.getText().toString();
    }
}
